package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.XFDGoodsDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.TotalNumListener;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：新发地   商铺   立即订购
 */
public class XFDShopAddCarAdapter extends RecyclerView.Adapter<XFDShopAddCarAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<XFDGoodsDetailsBeen.DataBean.ProductValueBean> list;
    private Context mContext;
    private TotalNumListener totalNumListener;


    public XFDShopAddCarAdapter(Context context, List<XFDGoodsDetailsBeen.DataBean.ProductValueBean> list ) {
        this.list = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_car_guige, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (holder.ettNum.getTag() instanceof TextWatcher) {
            holder.ettNum.removeTextChangedListener((TextWatcher) holder.ettNum.getTag());
        }
        holder.tvSpecifications.setText(list.get(position).getSuk());
        if (Double.parseDouble(list.get(position).getDiscount_price()) == 0) {
            holder.tvPrice.setText("¥"+list.get(position).getPrice());
        } else {
            holder.tvPrice.setText("¥"+list.get(position).getDiscount_price());
        }

        holder.ettNum.setText(list.get(position).getNum() + "");
        holder.ettNum.setSelection(holder.ettNum.getText().length());
        holder.tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    int num = Integer.parseInt(holder.ettNum.getText().toString());
                    list.get(position).setNum(++num);
                    holder.ettNum.setText(list.get(position).getNum() + "");

            }
        });
        holder.tvJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int num = Integer.parseInt(holder.ettNum.getText().toString());
                if (num > 0) {
                    list.get(position).setNum(--num);
                    holder.ettNum.setText(list.get(position).getNum() + "");
                }
             }
        });
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().isEmpty()) {
                    list.get(position).setNum(0);
                } else {
                    int num = Integer.parseInt(holder.ettNum.getText().toString());
                    list.get(position).setNum(num);
//                    holder.ettNum.setSelection(holder.ettNum.getText().length());
                }
                totalNumListener.onTotalNumListener();

            }
        };
        holder.ettNum.addTextChangedListener(textWatcher);

        holder.ettNum.setTag(textWatcher);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_specifications)
        TextView tvSpecifications;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.tv_jian)
        TextView tvJian;
        @BindView(R.id.ett_num)
        EditText ettNum;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


    public void setOnTotalNumListener(TotalNumListener totalNumListener) {
        this.totalNumListener = totalNumListener;

    }

}
